package com.rongshu.view.discovery.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.discovery.bean.ArticleListItemBean;
import com.rongshu.widget.ArticleLabelView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * created by zhengweis on 2018/11/1.
 * description：编辑推荐的文章列表
 */
public class EditorRecommendArticleListAdapter extends RecyclerView.Adapter<EditorRecommendArticleListAdapter.ViewHolder> {

    private Context context;
    private List<ArticleListItemBean> list;
    private AdapterListener listener;

    public EditorRecommendArticleListAdapter(Context context, List<ArticleListItemBean> list) {
        this.list = list;
        this.context = context;
    }

    public void setAdapterListener(AdapterListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_editor_recommend_article, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        //文字头图
        viewHolder.articleImg.setVisibility(TextUtils.isEmpty(list.get(i).getTopImg()) ? View.GONE : View.VISIBLE);
        GlideUtils.loadImageView(context, viewHolder.articleImg, list.get(i).getTopImg(),
                R.drawable.default_preview_icon, R.drawable.default_preview_icon);
        viewHolder.articleTitle.setText(list.get(i).getTitle());
        viewHolder.authorName.setText(list.get(i).getBi_name());
        viewHolder.articleContent.setText(list.get(i).getContents_header());
        viewHolder.articleLabel.bindDatas(list.get(i).getKeywordList());
        viewHolder.articleTitle.setTextColor(list.get(i).isRead() > 0 ?
                ContextCompat.getColor(context, R.color.color_898989) : ContextCompat.getColor(context, R.color.color_2c2c2c));
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    list.get(i).setRead(1);
                    notifyDataSetChanged();
                    listener.callBack(list.get(i));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.article_img)
        ImageView articleImg;
        @BindView(R.id.article_title)
        TextView articleTitle;
        @BindView(R.id.article_content)
        TextView articleContent;
        @BindView(R.id.author_name)
        TextView authorName;
        @BindView(R.id.article_label)
        ArticleLabelView articleLabel;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
